package edu.kaifadui.lmbase.controller;

import edu.kaifadui.lmbase.jwt.DevLoginToken;
import edu.kaifadui.lmbase.pojo.Major;
import edu.kaifadui.lmbase.pojo.Student;
import edu.kaifadui.lmbase.service.MajorService;
import edu.kaifadui.lmbase.service.StudentService;
import edu.kaifadui.lmbase.utils.Verification;
import edu.kaifadui.lmbase.vo.*;
import org.apache.commons.mail.EmailException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dualseason
 */
@RestController
public class StudentControllerImpl implements StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private MajorService majorService;

    @Override
    @PostMapping("/student")
    @DevLoginToken
    public Result saveStudent(@RequestBody Student student) {
        // 插入时候确保账户姓名、学号、密码不为空
        if (StringUtils.hasText(student.getName()) && StringUtils.hasText(student.getPassword())) {
            if (student.getNo().length() != 12) {
                return new Result(0, student, "请输入正确的学号");
            }
            if (student.getName().length() < 2 | student.getName().length() > 4) {
                return new Result(0, student, "请确保输入的姓名为2到4位，超过四位的请自行简写到四位，例如迪丽热巴");
            }
            Map<String, Object> map = new HashMap<>(1);
            map.put("no", student.getNo());
            List<Student> students = studentService.listByMap(map);
            if (students.size() > 0) {
                return new Result(0, student, "学号已存在");
            }
            student.setCreatetime(LocalDate.now().toString());
            student.setUpdatepasswordtime(LocalDate.now().toString());
            student.setGrade(student.getNo().substring(0,4));
            student.setStatus(1);
            HashMap<String, Object> major = new HashMap<>();
            String major_id = student.getNo().substring(4, 8);
            major.put("major_id",major_id);
            List<Major> majors = majorService.listByMap(major);
            if (!CollectionUtils.isEmpty(majors)){
                student.setMajor(majors.get(0).getMajorName());
                student.setClassroom(student.getGrade() + student.getMajor() + student.getNo().substring(9,10) + "班");
            }
            student.setPassword(DigestUtils.md5DigestAsHex((student.getPassword()+student.getCreatetime()).getBytes(StandardCharsets.UTF_8)));
            if (studentService.save(student)) {
                student.setPassword("保密");
                return new Result(200, student, "数据保存成功");
            }
            return new Result(0, student, "数据保存失败");
        }
        return new Result(0, student, "请确保账户名密码不为空");
    }

    @Override
    @DeleteMapping("/student")
    @DevLoginToken
    public Result deleteStudent(@RequestBody IdCode idCode) {
        return studentService.deleteStudent(idCode);
    }

    @Override
    @GetMapping("/student-all-Info-by-id")
    @DevLoginToken
    public Result getStudentAllInfoById(@RequestBody IdCode idCode) {
        return studentService.getStudentAllInfoById(idCode);
    }

    @Override
    @GetMapping("/student-contact-way")
    @DevLoginToken
    public Result getStudentContactWay(@RequestBody IdCode idCode) {
        return studentService.getStudentContactWay(idCode);
    }

    @Override
    @PostMapping("/check-is-user")
    @DevLoginToken
    public Result<Student> checkIsUser(@RequestBody Login login) {
        return studentService.checkIsUser(login);
    }

    @Override
    @GetMapping("/send-verification")
    @DevLoginToken
    public Result sendVerification(@RequestBody BandEmail bandEmail) {
        try {
            String s = Verification.SendVerification(bandEmail.getEmail());
            return new Result(200, s, "返回验证码");
        } catch (EmailException e) {
            return new Result(0, "邮箱绑定失败");
        }
    }

    @Override
    @PostMapping("/band-email")
    @DevLoginToken
    public Result bandEmail(@RequestBody BandEmail bandEmail) {
        return studentService.bandEmail(bandEmail);
    }

    @Override
    @PostMapping("/modify-password")
    @DevLoginToken
    public Result modifyPassword(@RequestBody Login login) {
        return studentService.modifyPassword(login);
    }

    @Override
    @PostMapping("/modify-gender")
    @DevLoginToken
    public Result modifyGender(@RequestBody ChangeGender gender) {
        return studentService.modifyGender(gender);
    }


}
